/*
   AD-22834: this controller/component is used for All Nat Ops page layouts as well
*/
public with sharing class CCIRCaseAttachmentsComponentController {

	public Case attachmentCase {get; set;}	
	public Id parentCaseId {get; set;}	
	public Boolean IsNato {get;set;}

	public CCIRCaseAttachmentsComponentController()	
	{
		IsNato = false;
	}
	
	private Boolean IsNATORecordType(String recordTypeName)
	{
		if(recordTypeName.contains('National Op'))
		   return true;
		   
		return false;
	}
	
	//get the combined comments for all related cases!
	public cAttachment[] attachments{
		get{
			List<cAttachment> relatedAttachments= new List<cAttachment>();
			id searchId= null;
			if(attachmentCase.parentId == null){
				searchId= attachmentCase.Id; 
			}
			else{
				searchId= parentCaseId;
			}
			
			for (Case caseAttachment  : [Select c.Id, c.parentId, c.CaseNumber, c.RecordType.Name,c.Owner.Name, c.Status, (Select Id, bodyLength, owner.Name, LastModifiedDate, Name, SystemModstamp, CreatedDate, CreatedBy.Name From Attachments Order By LastModifiedDate DESC) from Case c  where c.Id = :searchId or c.parentId = :searchId order by c.LastModifiedDate desc])
			{
				   // should only be one case per page - AD-22834
				   IsNato = IsNATORecordType(caseAttachment.RecordType.Name);
				
				   for(Attachment attach : caseAttachment.Attachments)				   
				   {
				   	   cAttachment tempcAttachment  = new cAttachment();
				       tempcAttachment.parentCaseId = caseAttachment.Id;				       
				       tempcAttachment.CaseNumber 		= caseAttachment.CaseNumber;
				   	   tempcAttachment.caserecordType 	= caseAttachment.RecordType.Name;
				   	   tempcAttachment.caseOwner 		= caseAttachment.Owner.Name;
				   	   tempcAttachment.caseStatus 		= caseAttachment.Status;
				       
				   	   tempcAttachment.FileName     = attach.Name;
				   	   tempcAttachment.attachmentId = attach.Id;
				   	   
				   	   tempcAttachment.size 		= EmailHelper.ConvertSize(integer.valueof(attach.bodyLength));
				   	   tempcAttachment.createdbyId 	= attach.owner.Id;
				   	   /*
				   	      updated by plo AD-22834, created by should read from created by field, not the owner of the attachment
				   	   */
				   	   tempcAttachment.createdbyName= attach.CreatedBy.name;
				   	   //tempcAttachment.createdbyName= attach.owner.name;
				   	   tempcAttachment.lastModifiedDate	= attach.LastModifiedDate.format();
				   	   tempcAttachment.createdDate = attach.CreatedDate.format();
				   	   relatedAttachments.Add(tempcAttachment);			   
				   }
				
			}
			if(relatedAttachments.size() == 0 )  
			   return null;
    	    else			   
			   return relatedAttachments;
		}
		
		set;
	}
		
	public PageReference AttachFile()
	{
		return null;
	}
	
	public PageReference deleteAttachment()
	{
		Id attachmentId = ApexPages.currentPage().getParameters().get('AttachmentId_d');
		
		//select the attachment to delete..
        List<Attachment> attachmentstoDelete= [Select Id, bodyLength, owner.Name, LastModifiedDate, Name, SystemModstamp From Attachment where Id=:attachmentId];
        		
        //delete the attachment (we are expecting only one attachment here, so this loop always executre once!)
		for(Attachment caseAttachment : attachmentstoDelete){
				delete caseAttachment;				
		}
		
		PageReference pg = new PageReference('/' + attachmentCase.Id);
		pg.setRedirect(true);
		return pg;
	}	
	
	  //Attachment object
  public class cAttachment{
	  	public ID parentCaseId {get; set;}
        public string caserecordType {get; set;}
        public String caseNumber {get; set;}
        public string caseOwner {get; set;}
        public string caseStatus  {get; set;}        
        public String status {get; set;}
	  	public ID attachmentId {get;set;}	  	
	  	public string fileName{get;set;}
	  	public string size{get;set;}
	  	public string createdById{get;set;}
	  	public string createdByName{get;set;}		
	  	public string lastModifiedDate{get;set;}  
	  	public String createdDate {get;set;}		 
  }    
	
}